package gui.visu.components.popupdisplaydata;

import gui.visu.listeners.ListenerPopupBand;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import controls.visu.BandControl;
import data.Constants;
import data.visu.datasets.A_DisplayDataDataSet;
import data.visu.datasets.BandDataSet;
import data.visu.structures.Range;
import data.visu.structures.RangeAndValueAndString;
import data.visu.structures.ValueAndString;
import data.visu.utils.ColorConvertor;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class PopupBand extends A_PopupDisplayData{

	private static final long serialVersionUID = -4522398480182409893L;


	public PopupBand(String name, BandControl controler) {
		super(controler);
		this.setControler(controler);
		this.setName(name+"PopupBandPane");
		ListenerPopupBand listener = new ListenerPopupBand(controler, this);
		addMouseListener(listener);
		addMouseMotionListener(listener);
	}

	@Override
	protected void drawDisplayData(A_DisplayDataDataSet DS, Graphics g) {
		int min, max;
		int xMinView, xMaxView;
		xMinView = controler.getxMinView();
		xMaxView = controler.getxMaxView();

		ArrayList<RangeAndValueAndString> displayedRangesOnRef = new ArrayList<RangeAndValueAndString>();

		Set<Entry<Range, Object>> safeMap = ((BandDataSet) DS).getRangeMap().entrySet();
		synchronized (safeMap) {
			for(Entry<Range, Object> entry : safeMap){
				RangeAndValueAndString rv = new RangeAndValueAndString(entry.getKey(), (ValueAndString) entry.getValue());
				min = rv.getRange().getMin();
				max = rv.getRange().getMax();
				//si intervalle en dehors de la vue
				if (max < xMinView || min > xMaxView || rv.getValue().getValue()==-1) {
					// on ne fait rien
				}
				else{
					displayedRangesOnRef.add(rv);
				}
			}
		}
		drawDisplayedRangesOnRef(g,displayedRangesOnRef);
	}


	private void drawDisplayedRangesOnRef(Graphics g, ArrayList<RangeAndValueAndString> displayedRangesOnRef) {

		int xMin, xMax;
		ValueAndString value;

		for(int i=0;i<displayedRangesOnRef.size();i++){
			RangeAndValueAndString rs = displayedRangesOnRef.get(i);
			xMin = convertXOnRef(rs.getRange().getMin());
			xMax = convertXOnRef(rs.getRange().getMax());
			value = rs.getValue();

			int rectHeight = this.getHeight()/((BandControl)controler).getBandType().getValues().length;
			int rectWidth = xMax-xMin;
			int yStart = this.getHeight()-(value.getValue()+1)*rectHeight;
			int yEnd = yStart+rectHeight;

			Color c = null;
			String s = null;

			HashMap<ValueAndString, Color> map = ((BandControl) controler).getNameAndColorMap();
			Iterator<ValueAndString> iter = map.keySet().iterator();
			while(iter.hasNext()){
				ValueAndString current = iter.next();
				if(current.getValue()==value.getValue()){
					c = ((BandControl) rs.getRange().getDisplayDataControl()).getNameAndColorMap().get(current);
					s = current.getString();
					break;
				}
			}

			g.setColor(c);
			g.fillRect(xMin, yStart, rectWidth, rectHeight);

			if(c!=null){
				if(((BandControl) controler).isDrawValuesInside()){
					//Affichage de la valeur pour ranges assez larges
					g.setFont(Constants.BAND_FONT_INSIDE_VALUES);
					FontMetrics metrics = g.getFontMetrics(Constants.BAND_FONT_INSIDE_VALUES);
					int letterHeight = metrics.getHeight();

					int time = xMax-xMin;
					int widthString = metrics.stringWidth(s);
					if(widthString+2<time){
						int middle = time/2;
						int xString = xMin+middle;
						int yString = (yEnd+yStart)/2;
						g.setColor(ColorConvertor.getOppositeColor(c));
						g.drawString(s, xString-widthString/2, (int) (yString+letterHeight/2));
					}
				}
			}
			else System.err.println("c = null !!!!");

			if(controler.isDrawVerticalBars()){
				g.setColor(Constants.DISPLAY_DATA_VERTICAL_BARS_COLOR);
				g.drawLine(xMin, yStart, xMin, yEnd);
				g.drawLine(xMax, yStart, xMax, yEnd);
			}
			if(controler.isDrawVerticalBars()){
				g.setColor(Constants.DISPLAY_DATA_HORIZONTAL_BARS_COLOR);
				g.drawLine(xMin, yStart, xMax, yStart);
				g.drawLine(xMin, yEnd, xMax, yEnd);
			}
		}	
	}



}
